package router

import (
	"cangqiong-takeout/src/handler"
	"cangqiong-takeout/src/service"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

type Route interface {
	Run() error
	Router() Route
}

type r struct {
	router *gin.Engine
}

func newRouter() *gin.Engine {
	return gin.Default()
}

// NewRouter 创建对外接口
func NewRouter() Route {
	return &r{
		router: newRouter(),
	}
}

func (rt *r) Router() Route {
	router := rt.router
	router.StaticFS("/stack", gin.Dir("./src/stack", true))
	admin := router.Group("/admin")
	{
		employee := admin.Group("/employee")
		{
			employee.POST("/login", service.EmployeeUserLogin)
			employee.POST("/logout", service.EmployeeUserLogout)
			employee.POST("", handler.HandlerFuncUser(), service.AddEmployeeUser)
			employee.GET("/page", handler.HandlerFuncUser(), service.FindEmployeeUser)
			employee.GET(":id", handler.HandlerFuncUser(), service.FindEmployeeUserToID)
			employee.POST("/status/:status", handler.HandlerFuncUser(), service.SetEmployeeUserStatus)
			employee.PUT("", handler.HandlerFuncUser(), service.EditEmployeeUserMessage)
			employee.PUT("/editPassword", handler.HandlerFuncUser(), service.EditEmployeePassWord)
		}
		category := admin.Group("/category")
		category.Use(handler.HandlerFuncUser())
		{
			category.GET("/page", service.GetCategoryMsg)
			category.GET("/list", service.GetCategoryList)
			category.POST("", service.AddCategory)
			category.PUT("", service.EditCategory)
			category.POST("/status/:status", service.SetCategoryStatus)
			category.DELETE("", service.DeleteCategory)
		}
		dish := admin.Group("/dish")
		dish.Use(handler.HandlerFuncUser())
		{
			dish.GET("/page", service.GetDishMsg)
			dish.GET("/list", service.GetDishList)
		}
	}
	return &r{router: router}
}

func (rt *r) Run() error {
	err := rt.router.Run(viper.GetString("sever.iprule") + ":" + viper.GetString("sever.port"))
	return err
}
